var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    moveStyle: 'left:0',
    currentIdx: 0,
    btnW: wx.getSystemInfoSync().windowWidth / 750 * 120,
    popupFlag: false,
    page: 1,
    limit: 10,
    btnFlag: false,
  },
  // 获取地址前缀
  getAddress() {
    var IAP = getApp().globalData.host;
    this.setData({
      IAP: IAP
    })
  },
  // 点击按钮，弹出/隐藏
  clickBtn(e) {
    this.setData({
      btnFlag: !this.data.btnFlag
    })
  },
  // 触摸开始
  touchS: function(e) {
    if (e.touches.length == 1) {
      this.setData({
        moveStyle: 'left:0',
        startX: e.touches[0].clientX,
        currentIdx: e.currentTarget.dataset.idx,
        currentType: e.currentTarget.dataset.type,
      })
    }
  },
  // 触摸移动
  touchM: function(e) {
    if (e.touches.length == 1) {
      var moveX = e.touches[0].clientX;
      var disX = this.data.startX - moveX
      var moveStyle = '';
      //如果移动距离小于等于0，文本层位置不变
      if (disX <= 0) {
        moveStyle = "left:0";
      } else if (disX > 0 && disX < 2 * this.data.btnW) {
        moveStyle = "left:-" + disX + "px";
      } else if (disX > 2 * this.data.btnW) {
        moveStyle = "left:-" + (2 * this.data.btnW) + "px";
      }
      this.setData({
        moveStyle: moveStyle
      })
    }
  },
  // 触摸结束
  touchE(e) {
    if (e.changedTouches.length == 1) {
      var endX = e.changedTouches[0].clientX;
      var disX = this.data.startX - endX
      var moveStyle = '';
      //如果移动距离小于等于0，文本层位置不变
      if (disX < this.data.btnW) {
        moveStyle = "left:0";
      } else if (disX >= this.data.btnW) {
        moveStyle = "left:-" + (2 * this.data.btnW) + "px";
      }
      this.setData({
        moveStyle: moveStyle,
        startX: 0
      })
    }
  },
  // 触摸打断
  touchC(e) {
    this.setData({
      moveStyle: 'left:0',
      startX: 0
    })
  },
  // 删除电杆、电缆
  deleteEngineering(e) {
    wx.showModal({
      title: '删除提醒',
      content: '是否确认删除本条记录',
      confirmColor: '#ff4c4c',
      confirmText: '删除',
      success: res => {
        var url = this.data.IAP;
        if (res.confirm) {
          if (e.currentTarget.dataset.type == '电杆' || e.currentTarget.dataset.type == '电缆') {
            url = url + 'electric/delete'
          } else if (e.currentTarget.dataset.type == '连接线' || e.currentTarget.dataset.type == '架空线') {
            url = url + 'cable/delete'
          }
          wx.request({
            url: url,
            header: {
              "token": wx.getStorageSync('token')
            },
            method: "POST",
            data: [e.currentTarget.dataset.idx],
            success: res => {
              if (res.data.code == 0) {
                var electric = this.data.electric.filter((value) => {
                  return value.electricId != e.currentTarget.dataset.idx
                })
                this.setData({
                  electric: electric,
                  currentIdx: -1
                })
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 关闭弹窗
  closePopup() {
    this.setData({
      popupFlag: false,
      popupFlagAdd: false,
    })
    wx.setStorageSync('projectId' + this.data.engProject.projectId, this.data.engProject.projectId)
    if (this.data.electric.length == 0) {
      this.setData({
        popupFlagAdd: true,
      })
      // wx.showModal({
      //   title: '提示信息',
      //   content: '该工程暂无数据，请尽快添加',
      // })
    }
  },
  // 根据工程ID获取工程信息
  getInfoById(eid) {
    wx.request({
      url: this.data.IAP + 'project/info/' + eid + '/',
      header: {
        "token": wx.getStorageSync('token')
      },
      success: res => {
        if (res.data.code == 0) {
          this.setData({
            engProject: res.data.engProject
          })
          wx.setNavigationBarTitle({
            title: res.data.engProject.projectName,
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },
  // 根据工程ID获取电杆/电缆信息
  getElectricListById(eid) {
    wx.request({
      url: this.data.IAP + 'electric/list',
      header: {
        "token": wx.getStorageSync('token')
      },
      data: {
        page: this.data.page,
        limit: this.data.limit,
        projectId: eid
      },
      success: res => {
        if (res.data.code == 0) {
          var newLength = Math.ceil(res.data.page.list.length / 9);
          var electric = []
          for (var i = 0; i < newLength; i++) {
            var sliceStart = i * 9,
              sliceEnd = i * 9 + 9
            electric.push(res.data.page.list.slice(sliceStart, sliceEnd))
          }
          this.setData({
            electric: electric
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getAddress();
    this.getInfoById(options.eid)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      moveStyle: 'left:0',
    })
    setTimeout(() => {
      var currentTime = new Date().getTime();
      var endTime = new Date(this.data.engProject.endTime.replace(/-/g, "/")).getTime();
      var number = Math.ceil((endTime - currentTime) / (24 * 3600 * 1000))
      this.setData({
        number: number
      })
      if (wx.getStorageSync('projectId' + this.data.engProject.projectId)) {
        this.setData({
          popupFlag: false
        })
      } else {
        this.setData({
          popupFlag: true
        })
      }
    }, 500)
    setTimeout(() => {
      this.getElectricListById(this.data.engProject.projectId);
    }, 600)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    setTimeout(() => {
      this.setData({
        btnFlag: false
      })
    }, 300)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})